{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Jupyter Notebooks\n",
    "\n",
    "**Time**\n",
    "- Teaching: 10 min\n",
    "- Challenges: 0\n",
    "\n",
    "**Questions**\n",
    "- How do I use a Jupyter Notebook?\n",
    "\n",
    "**Learning Objectives**:\n",
    "- Launch the Jupyter Notebook, create new notebooks, and exit the Notebook.\n",
    "- Create Markdown cells in a notebook.\n",
    "- Create and run Python cells in a notebook.\n",
    "* * * * *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction\n",
    "\n",
    "You've just got a basic intro to programming in python. Now, we are going to get our hands dirty, but...\n",
    "\n",
    "**Don't panic!**\n",
    "\n",
    "*You can't break anything!* You can always get another copy of any notebook from the workshop on the [github repo](https://github.com/dlab-berkeley/python-intensive)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Navigating in Jupyter Notebook\n",
    "\n",
    "Jupyter Notebooks have more useful features for interactive use than the standard python interpreter, but they work in the same basic way: you type things and then execute them.\n",
    "\n",
    "Unlike many graphical systems, there is no button to run your code! Instead, **you run code using Shift-Enter**. This also moves you to the next box (or \"cell\") for code below the one you just ran (but this may change in the future).\n",
    "\n",
    "Try to **run the following code using Shift-Enter** now!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Hello World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you hit **Enter** only, Jupyter Notebook gives you another line in the current cell.\n",
    "\n",
    "This allows you to [compose](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#compose) multi-line commands and submit them to python all at once."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1 + 2\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Control-Enter** executes the cell and does not move to the next cell.\n",
    "\n",
    "You can enter the same line over and over again into the interpreter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try entering this cell a few times:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = i + 1\n",
    "print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to create new empty cells, you can use Insert -> Insert Cell Below or use the Insert Cell Below button at the top of the notebook. Try entering a new cell below this one."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also split cells by putting your cursor on the line you want to split, and clicking on Edit --> Split cell. Split the cell below after the `c = 2` line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "b = 2\n",
    "c = 2\n",
    "d = a + b + c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You merge cells by clicking the Edit --> Merge cell Above / Below. Go back to the cells you split and merge them back into one cell."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Markdown\n",
    "\n",
    "Jupyter notebooks allow you type in markdown as well as code. In fact, this very cell is written in Markdown!\n",
    "\n",
    "Markdown is a lightweight markup language with plain text formatting syntax designed so that it can be converted to HTML and many other formats. Markdown has its own syntax, but it's easy to learn. Here's a [cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) that can help.\n",
    "\n",
    "Double click the cell below to see the markdown code rendering the output."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "****\n",
    "\n",
    "*   Use asterisks\n",
    "*   to create\n",
    "*   bullet lists.\n",
    "\n",
    "but\n",
    "\n",
    "1.  Use numbers\n",
    "2.  to create\n",
    "3.  numbered lists.\n",
    "\n",
    "# A Level-1 Heading\n",
    "## B Level-2 Heading\n",
    "### C Level-3 Heading (etc)\n",
    "\n",
    "Line breaks\n",
    "don't matter.\n",
    "\n",
    "But blank lines\n",
    "create new paragraphs.\n",
    "\n",
    "[Create links](http://dlab.berkeley.edu) with `[...](...)`.\n",
    "\n",
    "****"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, cells in jupyter notebook are code cells, which means it's expecting you to enter Python code. But you can change the cell format by changing the dropdown item on the top from \"Code\" to \"Markdown\".\n",
    "\n",
    "Enter some markdown text in the cell below, and execute it. Be sure to change the cell from code to markdown!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Keyboard shortcuts\n",
    "\n",
    "There are many useful keyboard shortcuts in Jupyter Notebooks. Check some of them out [here](https://www.cheatography.com/weidadeyue/cheat-sheets/jupyter-notebook/)! Below are five essential ones. \n",
    "* When you click a **code** cell, the border is green (\"editing mode\") and means you can edit code in that cell. \n",
    "* We first need to turn this green border to blue (\"command mode\") before we can use the shortcuts (**markdown** cell borders are blue by default). \n",
    "\n",
    "Press the `Esc` key to ensure that a code cell border is blue, or simply click a markdown cell: \n",
    "\n",
    "1. Press the `a` key to add a cell above the current one\n",
    "2. Press `b` to add a cell below the current one\n",
    "3. Press `m` to convert the cell to markdown\n",
    "4. Press `y` to convert the cell to code\n",
    "5. Press `dd` to delete the current cell"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Error Reporting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What happens if you run this cell?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Or am I)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, Jupyter offers proper error reporting. But don't worry - Python is just trying to help fix something it can't understand!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## History"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to see all the commands you've run so far in the notebook, use the `%history` built-in command. This is unique to Jupyter Notebook. See more [here](http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-history)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%history"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to see your history for Python in interactive mode in the shell, check out the answers given [here](https://stackoverflow.com/questions/6558765/how-do-you-see-the-entire-command-history-in-interactive-python)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Clearing Jupyter\n",
    "\n",
    "Jupyter remembers everything it executed, **even if it's not currently displayed in the notebook**.\n",
    "\n",
    "To clear everything from Jupyter use Kernel-->Restart in the menu."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mystring = \"And three shall be the count.\" \n",
    "\n",
    "print(mystring)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now use Kernel-->Restart in the menu!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(mystring)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the error message contains a recap of the input that caused the error (with an arrow, no less!)   It is objecting that **mystring** is not defined, since we just reset it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tab Completion\n",
    "\n",
    "Jupyter also allows for tab completion, just like many command line interpreters and text editors like Sublime Text. The way this works is that if you begin typing the name of something (e.g., a variable, a file in the current directory, etc.) that already exists, you can simply hit `TAB` and Jupyter will complete it for you. If there is more than one possibility, it will show them to you and you can choose from there. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_me = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now simply type in `test` (or even just `te`) and hit `TAB`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_me_2 = 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now try typing `te` and see what happens when you hit `TAB`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Commenting\n",
    "\n",
    "We will discuss how and why to comment code later in this series, but it's also useful when you temporarily don't want to run a section of code.\n",
    "\n",
    "Simply place a pound sign `#` at the beginning of the line, and that line won't run.\n",
    "\n",
    "Try running the cell below, then comment out `bad_thing`, and run it again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "good_thing = 1+1\n",
    "\n",
    "bad_thing = 'a' * 'b'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can comment or uncomment multiple lines of code at once by selecting them and then hitting `Control + /`. Try it below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "good_thing = 1+1\n",
    "\n",
    "bad_thing_1 = 'a' * 'b'\n",
    "bad_thing_2 = good_thing[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Indenting\n",
    "\n",
    "Consistent indentation is essential in Python.\n",
    "\n",
    "To move multiple lines of code at once, you can select them and then hit `Control + ]` to indent them (move to the right), or `Control + [` to dedent them to the left.\n",
    "\n",
    "For Macs use `Command` in place of `Control`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "move_me = 1\n",
    "move_me_too = 'abc'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exiting Jupyter\n",
    "\n",
    "When you close your Jupyter notebook window, all of your values will be lost. But you can save your code for a later time.\n",
    "\n",
    "First go to File --> Close and Halt in order to shutdown the notebook you are using. Once all notebooks are shutdown, to end the Jupyter server, go back to the Anaconda Navigator and shut it down. You may get a warning dialog box alerting you that Jupyter Notebook is still running. Just click **Quit** to shut everything down."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Challenge 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Clear this notebook again.\n",
    "- Save the notebook.\n",
    "- Close this notebook correctly.\n",
    "- Shutdown Jupyter notebook. Reopen Jupyter notebook and open the notebook 03_Variables_Assignment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
